﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EHR.Common;
using CommunitySystem.PageView.晋升管理;
using System.Collections.Generic;
using CommunitySystem.Infrastructure.CommonMethod;
using CommunitySystem.PageView;
using CommunitySystem.Ctrls;

namespace CommunitySystem
{
	public partial class PromotControl : UserControl
	{
        private readonly JinShengService.JinShengServiceClient _client = null;
        
		public PromotControl()
		{
			// 为初始化变量所必需
			InitializeComponent();
           
            _client = new JinShengService.JinShengServiceClient();
            _client.GetOrgForHRCompleted += (sender, e) =>
            {
                
                if (JinShenConst.Administrator.Contains(e.Result))
                {
                    btnAddCandidator.Visibility = System.Windows.Visibility.Visible;
                    btnCheck.Visibility = System.Windows.Visibility.Visible;
                    btnCheckCandidator.Visibility = System.Windows.Visibility.Visible;
                    btnDelete.Visibility = System.Windows.Visibility.Visible;
                    btnEdit.Visibility = System.Windows.Visibility.Visible;
                    btnKaoCha.Visibility = System.Windows.Visibility.Visible;
                    btnNew.Visibility = System.Windows.Visibility.Visible;
                    //btnSelectCandidator.Visibility = System.Windows.Visibility.Visible;
                    btnValation.Visibility = System.Windows.Visibility.Visible;
                }
                if (JinShenConst.HR.Contains(e.Result))
                {
                    btnAddCandidator.Visibility = System.Windows.Visibility.Visible;
                    //btnCheck.Visibility = System.Windows.Visibility.Visible;
                    btnDelete.Visibility = System.Windows.Visibility.Visible;
                    btnEdit.Visibility = System.Windows.Visibility.Visible;
                    btnKaoCha.Visibility = System.Windows.Visibility.Visible;
                    btnNew.Visibility = System.Windows.Visibility.Visible;
                    btnSelectCandidator.Visibility = System.Windows.Visibility.Visible;
                    btnValation.Visibility = System.Windows.Visibility.Visible;
                }
            };
            _client.GetOrgForHRAsync(Session.LoginUser.LoginName);
            _client.GetJinShen_MainInfoCompleted += new EventHandler<JinShengService.GetJinShen_MainInfoCompletedEventArgs>(_client_GetJinShen_MainInfoCompleted);
            _client.DeleteJinShenMainInfoCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功!");
                    Refresh();
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("删除失败!");
                }
            };
            Refresh();
		}

        private void Refresh()
        {
            _client.GetJinShen_MainInfoAsync("1=1", Session.LoginUser.LoginName);
        }

        void _client_GetJinShen_MainInfoCompleted(object sender, JinShengService.GetJinShen_MainInfoCompletedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = e.Result;
        }

        private PromotWindow window = null;

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageButton1_Click(object sender, RoutedEventArgs e)
        {


            if (JinShenConst.CanAction("", JinShenConst.ACTION_Solution_New))
            {
                window = new PromotWindow(0, JinShenConst.ACTION_Solution_New);
                window.Show();
                window.Closed += new EventHandler(window_Closed);
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + JinShenConst.ACTION_Solution_New + "操作");
            }           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void window_Closed(object sender, EventArgs e)
        {
            Refresh();

        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageButton2_Click(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShengService.JinShen_MainInfo;

                if (JinShenConst.CanAction(item.Status, JinShenConst.ACTION_Solution_Modify))
                {
                    window = new PromotWindow(item.ID, JinShenConst.ACTION_Solution_Modify);
                    window.Show();
                    window.Closed += new EventHandler(window_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + JinShenConst.ACTION_Solution_Modify + "操作");
                }

                /////只有处于提交和退回状态，才可以编辑
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Retire)
                //{
                //    window = new PromotWindow(item.ID, "编辑");
                //    window.Show();
                //    window.Closed += new EventHandler(window_Closed);
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.InfoMessage(item.Status + "状态不能编辑");
                //}
                
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageButton3_Click(object sender, RoutedEventArgs e)
        {
            ///只有提交和退回状态能删除
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShengService.JinShen_MainInfo;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认删除?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                    cm.Show();
                    cm.OKButton.Click += (sender1, e1) =>
                    {
                        _client.DeleteJinShenMainInfoAsync(item);
                    };
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }

                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Retire)
                //{
                //    SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认删除?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                //    cm.Show();
                //    cm.OKButton.Click += (sender1, e1) =>
                //    {
                //        _client.DeleteJinShenMainInfoAsync(item);
                //    };
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能删除");
                //    return;
                //}
            }
        }

       /// <summary>
       /// 审核
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void imageButton4_Click(object sender, RoutedEventArgs e)
        {

            ///只有状态为提交 和 审核通过 或者审核失败， 才能审核
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShengService.JinShen_MainInfo;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    PromotCheckWindow wind = new PromotCheckWindow(item);
                    wind.Show();
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit 
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Retire 
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_SolutionPass)
                //{
                //    PromotCheckWindow wind = new PromotCheckWindow(item);
                //    wind.Show();
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能审核");
                //    return;
                //}
            }
         
        }

        /// <summary>
        /// 增加候选人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShengService.JinShen_MainInfo;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    window = new PromotWindow(item.ID, "新增候选人");
                    window.Show();
                    window.Closed += new EventHandler(window_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                /////只有审核通过 和  人员审核不通过可以进入增加
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_SolutionPass)
                //{
                //    window = new PromotWindow(item.ID, "新增候选人");
                //    window.Show();
                //    window.Closed += new EventHandler(window_Closed);
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能增加候选人");
                //    return;
                //}
            }
        }


        /// <summary>
        /// 民主测评及竞争上岗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click_1(object sender, RoutedEventArgs e)
        {
            ///候选人审核 状态才可以
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShengService.JinShen_MainInfo;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    CandidatorCheckWindow window = new CandidatorCheckWindow(item,"",false);
                    window.Show();
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_CandidateCheck_Pass || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Approved)
                //{
                //    CandidatorCheckWindow window = new CandidatorCheckWindow(item);
                //    window.Show();
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能进行民主测评");
                //    return;
                //}
            }
        }

        /// <summary>
        /// 人员核定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click_2(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShengService.JinShen_MainInfo;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    ChooseCandidatorWindow window = new ChooseCandidatorWindow(item,false);

                    window.Show();
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_SolutionPass || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_CandidateCheck_UnPass)
                //{
                //    ChooseCandidatorWindow window = new ChooseCandidatorWindow(item);
                    
                //    window.Show();
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能进行民主测评");
                //    return;
                //}
            }
           

        }

        /// <summary>
        /// 考察管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgBtn8_Click(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShengService.JinShen_MainInfo;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, JinShenConst.ACTION_Candidate_Inspect))
                {
                    FlipSwitchForJinShen jinshen = new FlipSwitchForJinShen(item);
                    CommunitySystem.Infrastructure.CommonMethod.TabControlMethod.
                         addNewTabItem(jinshen, "考察管理", item.ID + "考察管理", App.currentTabControl,
                                       App.Current.Resources["tabItemStyle"] as Style);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Approved
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Inspect
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Party_Meeting
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Show
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Apply
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree)
                //{
                //    FlipSwitchForJinShen jinshen = new FlipSwitchForJinShen(item);
                //    CommunitySystem.Infrastructure.CommonMethod.TabControlMethod.
                //         addNewTabItem(jinshen, "考察管理", item.ID + "考察管理", App.currentTabControl,
                //                       App.Current.Resources["tabItemStyle"] as Style);
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能进行民主测评");
                //    return;
                //}
            }
            
        }

        
        /// <summary>
        /// 人员审核
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheckCandidator_Click(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShengService.JinShen_MainInfo;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    PromotCheckWindow wind = new PromotCheckWindow(item);
                    wind.Show();
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                
            }
        }
	}
}